import json  #解析json
import re   #正则表达式
import pandas as pd  #导入Excel

# 打开并加载 JSON 文件
with open('职位.json', 'r', encoding='utf-8') as f:
    data = json.load(f)

# # 创建一个列表来存储所有职位信息
jobs_data = []

# 遍历 jobList 并提取所需信息
for job in data:
    job_name = job.get('jobName', '')
    salary_desc = job.get('salaryDesc', '')
    job_labels = job.get('jobLabels', [])
    skills = job.get('skills', [])
    job_degree = job.get('jobDegree', '')
    city_name = job.get('cityName', '')
    brand_name = job.get('brandName', '')
    welfare_list = job.get('welfareList', [])

    # 使用正则表达式匹配工资范围，并提取下限值
    salary_match = re.match(r'(\d+)-(\d+)K', salary_desc)
    if salary_match:
        salary_min = int(salary_match.group(1)) * 1000  # 将下限值转换为整数（单位：元）
    else:
        salary_min = None  # 如果格式不符合，则设置为 None 或其他默认值

    processed_job = {
        'jobName': job_name,
        'salaryDesc': salary_desc,
        'salaryMin': salary_min,
        'jobLabels': ', '.join(job_labels),
        'skills': ', '.join(skills),
        'jobDegree': job_degree,
        'cityName': city_name,
        'brandName': brand_name,
        'welfareList': ', '.join(welfare_list)
    }

    jobs_data.append(processed_job)
    print(processed_job)
# 创建 DataFrame
df = pd.DataFrame(jobs_data)

# 保存到 Excel 文件
df.to_excel('jobs.xlsx', index=False)

print("数据已成功保存到 jobs.xlsx 文件中。")